package com.jack.blog.service;

import com.jack.blog.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContext;
import java.io.File;
import java.util.Arrays;
import java.util.List;

@Component("config")
public class BlogConfig {
    private final static String LIST_SEPARATOR=",";
    @Value("${server.host}")
    private String host;
    @Value("${server.port}")
    private int port;
    @Value("${blog.pageSize}")
    private int pageSize;
    @Value("${blog.article.allow}")
    private String blogAllow;
    @Value("${blog.cover.allow}")
    private String blogCoverAllow;
    @Value("${blog.article.location}")
    private String blogLocation;
    @Value("${blog.cover.location}")
    private String coverLocation;
    @Value("${user.avatar.location}")
    private String avatarLocation;
    @Value("${user.avatar.allow}")
    private String userAvatarAllow;
    @Value("${blog.article.url}")
    private String articleUrl;
    @Value("${blog.cover.url}")
    private String coverUrl;
    @Value("${user.avatar.url}")
    private String avatarUrl;

    @Autowired
    private ServletContext servletContext;

    public String getHost() {
        return host;
    }

    public int getPort() {
        return port;
    }

    public int getPageSize() {
        return pageSize;
    }

    public String getArticleUrl() {
        return articleUrl;
    }

    public String getCoverUrl() {
        return coverUrl;
    }

    public String getAvatarUrl() {
        return avatarUrl;
    }

    public File getBlogDirectory()throws Exception {
        //File file = new File(servletContext.getRealPath(blogLocation));
        File file=new File(blogLocation);
        FileUtil.checkDirectory(file);
        return file;
    }

    public File getCoverDirectory()throws Exception {
        //File file = new File(servletContext.getRealPath(coverLocation));
        File file=new File(coverLocation);
        FileUtil.checkDirectory(file);
        return file;
    }

    public File getAvatarDirectory() throws Exception{
        //File file = new File(servletContext.getRealPath(avatarLocation));
        File file=new File(avatarLocation);
        FileUtil.checkDirectory(file);
        return file;
    }

    public List<String> getBlogAllow() {
        return Arrays.asList(blogAllow.split(LIST_SEPARATOR));
    }

    public List<String> getBlogCoverAllow() {
        return Arrays.asList(blogCoverAllow.split(LIST_SEPARATOR));
    }

    public List<String> getUserAvatarAllow() {
        return Arrays.asList(userAvatarAllow.split(LIST_SEPARATOR));
    }
}
